package com.dianping.index;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


public class WriteIndex
{
    private static final String FILE_DIR="E:\\docs";
    private static final String INDEX_DIR="E:\\index";
    private final static Logger logger = Logger.getLogger(WriteIndex.class);
    public static void main(String[] args){
        long startTime=System.currentTimeMillis();
        logger.error(startTime);
        int numIndexed = index(new File(FILE_DIR),new File(INDEX_DIR));
        System.out.println(numIndexed);
        long finishTime=System.currentTimeMillis();
        logger.error(finishTime-startTime);
        //这个字体还行，日志字数管用吗???
    }
    @SuppressWarnings("deprecation")
    public static int index(File srcDir,File saveDir){
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
        IndexWriter indexWriter = null;
        Directory directory = null;
        
        int numedIndex=0;
        try{
            directory = FSDirectory.open(saveDir);
            indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
            indexWriter.setMergeFactor(100);
            indexWriter.setMaxMergeDocs(1000);
            indexDirectory(indexWriter,srcDir);
            
            numedIndex=indexWriter.numDocs();
            indexWriter.setUseCompoundFile(true);
            indexWriter.optimize();
        }catch(IOException ex){
            logger.error("建立索引失败", ex);
        }finally{
            if(indexWriter!=null){
                try
                {
                    indexWriter.close();
                }
                catch (CorruptIndexException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if(directory!=null){
                try
                {
                    directory.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return numedIndex;
    }
    
    public static void indexDirectory(IndexWriter indexWriter,File srcDir)throws IOException{
        File[] files = srcDir.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                indexDirectory(indexWriter,file);
            }else if(file.getName().endsWith(".txt")){
                indexFile(indexWriter,file);
            }
        }
    }
    
    public static void indexFile(IndexWriter indexWriter,File file){
        if(file.isHidden()||!file.canRead()||!file.exists()){
            logger.error(file.getName()+"不可访问");
            return;
        }
        try
        {
            String path = file.getCanonicalPath();
            System.out.println(""+path);
            String text = loadFileToString(file);
            Document document = new Document();
            //document.add(new Field("contents", text, Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("contents", text, Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
            document.add(new Field("contents", new FileReader(file)));
            document.add(new Field("filename", path, Field.Store.YES, Field.Index.NOT_ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
            indexWriter.addDocument(document);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static String loadFileToString(File file){
        BufferedReader br=null;
        try{
            br = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            while(line!=null){
                sb.append(line);
                line=br.readLine();
            }
            return sb.toString();
        }
        catch (Exception e)
        {
            logger.error("系统异常", e);
            return null;
        }finally{
            try{
                br.close();
            }catch(IOException ioe){
                logger.error("关闭bufferedReader异常", ioe);
            }
        }
    }
    
}
